package net.pws.oos.web.dto;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import net.pws.common.beanutils.BeanUtils;
import net.pws.common.persistence.EntityManager;
import net.pws.oos.biz.model.Checkin;
import net.pws.oos.biz.model.Organization;
import net.pws.oos.biz.model.User;

import org.apache.commons.lang.StringUtils;

public class CheckinDto extends Checkin {

	private static final long serialVersionUID = -2469561115861938092L;

	public static CheckinDto from(Checkin model) {
		String[] ignoredProps = { "executeOrganization", "executePerson" };
		CheckinDto dto = new CheckinDto();
		BeanUtils.copyProperties(dto, model, ignoredProps);
		if (model.getExecutePerson() != null) {
			dto.setExecPersonId(model.getExecutePerson().getId());
			dto.setExecPersonName(model.getExecutePerson().getName());
		}
		if (model.getExecuteOrganization() != null) {
			dto.setExecOrganId(model.getExecuteOrganization().getId());
			dto.setExecOrganName(model.getExecuteOrganization().getName());
		}
		return dto;
	}

	public static List<CheckinDto> from(List<Checkin> models) {
		List<CheckinDto> list = new ArrayList<CheckinDto>();
		for (Checkin model : models) {
			list.add(from(model));
		}
		return list;
	}

	@SuppressWarnings("rawtypes")
	public static CheckinDto from(Map map) {
		CheckinDto dto = new CheckinDto();
		BeanUtils.copyProperties(dto, map);
		return dto;
	}

	public static Checkin to(CheckinDto dto, Checkin model,
			EntityManager entityManager) {
		if (dto == null) {
			return null;
		}

		if (model == null) {
			model = new Checkin();
		}
		String[] ignoredProps = { "id", "executeOrganization", "executePerson" };
		BeanUtils.copyProperties(model, dto, ignoredProps);

		if (StringUtils.isEmpty(dto.getExecOrganId())) {
			model.setExecuteOrganization(null);
		} else {
			model.setExecuteOrganization(entityManager.find(Organization.class,
					dto.getExecOrganId()));
		}

		if (StringUtils.isEmpty(dto.getExecPersonId())) {
			model.setExecutePerson(null);
		} else {
			model.setExecutePerson(entityManager.find(User.class,
					dto.getExecPersonId()));
		}

		if (model.getExecuteOrganization() == null
				&& model.getExecutePerson() != null) {
			model.setExecuteOrganization(model.getExecutePerson()
					.getOrganization());
		}
		return model;
	}

	private String execOrganId;

	private String execOrganName;

	private String execPersonId;

	private String execPersonName;

	private List<AttachmentDto> attachments;

	public String getExecOrganId() {
		return execOrganId;
	}

	public void setExecOrganId(String execOrganId) {
		this.execOrganId = execOrganId;
	}

	public String getExecOrganName() {
		return execOrganName;
	}

	public void setExecOrganName(String execOrganName) {
		this.execOrganName = execOrganName;
	}

	public String getExecPersonId() {
		return execPersonId;
	}

	public void setExecPersonId(String execPersonId) {
		this.execPersonId = execPersonId;
	}

	public String getExecPersonName() {
		return execPersonName;
	}

	public void setExecPersonName(String execPersonName) {
		this.execPersonName = execPersonName;
	}

	public List<AttachmentDto> getAttachments() {
		return attachments;
	}

	public void setAttachments(List<AttachmentDto> attachments) {
		this.attachments = attachments;
	}

}
